package com.fbs.wowo.forum.service.imp;

import com.fbs.wowo.forum.entity.Comments;
import com.fbs.wowo.forum.mapper.CommentsMapper;
import com.fbs.wowo.forum.service.CommentsService;
import com.fbs.wowo.forum.vo.CommentsList;
import com.fbs.wowo.forum.vo.CommentsListQuery;
import com.fbs.wowo.util.common.Constants;
import com.fbs.wowo.util.common.Generator;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("commentsService")
@Transactional
public class CommentsServiceImp implements CommentsService {

    /**
     * 添加评论 添加完评论返回新添加的评论信息？
     *
     * @param comments 评论
     */
    @Override
    public Integer add(Comments comments) {
        // TODO 各种判空
        comments.setId(Generator.generateId());
        comments.setStatus(Constants.COMMENTS_STATUS_NORMAL);
        comments.setCommentTime(new Date());
        commentsMapper.insertSelective(comments);

        return this.count(comments.getCommentEntity(), comments.getCommentType());
    }

    /**
     * 某文章下的评论列表
     * 完整需求
     * 1. 分页
     * 2. 用户头像，名称 时间，发送者id
     * 3. 综合排序
     * 4. 能删除自己的评论
     * 5. 能点赞并取消点赞评论
     * 6. 能回复
     * 7. 能查看回复列表
     * 删减需求
     * 1. 分页
     * 2. 用户头像，名称
     * 3. 能删除自己的评论
     *
     * @param query 评论列表查询筛选
     * @return 评论列表
     */
    @Override
    public List<CommentsList> list(CommentsListQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<CommentsList> list = commentsMapper.list(query.getEntityId(), query.getEntityType(), Constants.COMMENTS_STATUS_NORMAL);
        return list;
    }

    @Override
    public void del(String commentId) {
        Comments comments = commentsMapper.selectByPrimaryKey(commentId);
        comments.setStatus(Constants.COMMENTS_STATUS_DEL);
        commentsMapper.updateByPrimaryKeySelective(comments);
    }

    /**
     * 获取评论数
     *
     * @param entityId
     * @param entityType
     */
    @Override
    public Integer count(String entityId, Integer entityType) {
        return commentsMapper.count(entityId, entityType);
    }

    @Autowired
    private CommentsMapper commentsMapper;
}
